package com.xcf.queue;

import javax.xml.bind.SchemaOutputResolver;
import java.util.LinkedList;
import java.util.Queue;

/**
 * NumLand <br>
 * <p>
 * 给你一个由 '1'（陆地）和 '0'（水）组成的的二维网格，请你计算网格中岛屿的数量。
 * <p>
 * 岛屿总是被水包围，并且每座岛屿只能由水平方向或竖直方向上相邻的陆地连接形成。
 * <p>
 * 此外，你可以假设该网格的四条边均被水包围。
 * <p>
 * <p>
 * <p>
 * 示例 1:
 * <p>
 * 输入:
 * [
 * ['1','1','1','1','0'],
 * ['1','1','0','1','0'],
 * ['1','1','0','0','0'],
 * ['0','0','0','0','0']
 * ]
 * 输出: 1
 * 示例 2:
 * <p>
 * 输入:
 * [
 * ['1','1','0','0','0'],
 * ['1','1','0','0','0'],
 * ['0','0','1','0','0'],
 * ['0','0','0','1','1']
 * ]
 * 输出: 3
 * 解释: 每座岛屿只能由水平和/或竖直方向上相邻的陆地连接而成。
 * <p>
 * https://leetcode-cn.com/explore/learn/card/queue-stack/217/queue-and-bfs/872/
 *
 * @author xiaocf <br>
 * @version 1.0 <br>
 * @date 2020/7/30 9:21 <br>
 */
public class NumLand {

    public int numIslandsByBfs(char[][] grid) {
        if (grid == null || grid.length == 0) {
            return 0;
        }
        // 行
        int row = grid.length;
        // 列
        int col = grid[0].length;
        int landNum = 0;
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                if (grid[i][j] == '1') {
                    landNum++;
                    Queue<String> tempQueue = new LinkedList<>( );
                    String node = i + "_" + j;
                    grid[i][j] = '0';
                    tempQueue.offer(node);
                    // 开始BFS
                    while (!tempQueue.isEmpty( )) {
                        String currNode = tempQueue.poll( );
                        String[] split = currNode.split("_");
                        Integer cRow = Integer.valueOf(split[0]);
                        Integer cCol = Integer.valueOf(split[1]);
                        if (cCol > 0 && grid[cRow][cCol - 1] == '1') {
                            // 左
                            tempQueue.offer(cRow + "_" + (cCol - 1));
                            grid[cRow][cCol - 1] = '0';
                        }
                        if (cRow > 0 && grid[cRow - 1][cCol] == '1') {
                            // 上
                            tempQueue.offer((cRow - 1) + "_" + cCol);
                            grid[cRow - 1][cCol] = '0';
                        }
                        if (cCol < col - 1 && grid[cRow][cCol + 1] == '1') {
                            // 右
                            tempQueue.offer(cRow + "_" + (cCol + 1));
                            grid[cRow][cCol + 1] = '0';
                        }
                        if (cRow < row - 1 && grid[cRow + 1][cCol] == '1') {
                            // 下
                            tempQueue.offer((cRow + 1) + "_" + cCol);
                            grid[cRow + 1][cCol] = '0';
                        }
                    }
                }
            }
        }
        return landNum;
    }

    public static void main(String[] args) {
        NumLand numLand = new NumLand( );
        int islands;
        /* islands = numLand.numIslandsByBfs(new char[][]{
                {'1', '1', '1', '1', '0'},
                {'1', '1', '0', '1', '0'},
                {'1', '1', '0', '0', '0'},
                {'0', '0', '0', '0', '0'}
        });
        System.out.println("islands = " + islands);*/

        islands = numLand.numIslandsByBfs(new char[][]{
                {'1', '1', '0', '0', '0'},
                {'1', '1', '0', '0', '0'},
                {'0', '0', '1', '0', '0'},
                {'0', '0', '0', '1', '1'}
        });
        System.out.println("islands = " + islands);
    }
}
